package test;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class StringProcessor {

	private ObjectPool<StringFormat> pool;

	public StringProcessor(ObjectPool<StringFormat> pool) {
		this.pool = pool;
	}

	/**
	 * Dumps the contents of the {@link Reader} to a String, closing the
	 * {@link Reader} when done.
	 */
	public void process(List<String> strList) {
		for (String str : strList) {
			Thread thread = new StringProcessThread(pool, str);
			thread.start();
		}

		// 设置等待两秒，等待线程结束
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {

		GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
		conf.setMaxTotal(3);
		conf.setMaxIdle(2);
		conf.setMinIdle(2);

		StringProcessor stringProcessor = new StringProcessor(new GenericObjectPool<StringFormat>(new StringFormatFactory(), conf));
		List<String> strList = new ArrayList<String>();
		strList.add("123");
		strList.add("456");
		strList.add("789");
		strList.add("111");
		strList.add("222");
		strList.add("333");
		strList.add("333");
		strList.add("333");
		strList.add("333");
		strList.add("333");
		stringProcessor.process(strList);
	}
}